<template>
	<div class="sys-warehouse-container">
		<el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="700px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="仓库名称" prop="name" :rules="[{ required: true, message: '仓库名称不能为空', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.name" placeholder="仓库名称" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="仓库代码" prop="code" :rules="[{ required: true, message: '仓库代码不能为空', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.code" placeholder="仓库代码" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20" >
						<el-form-item label="是否启用" prop="isEnable" :rules="[{ required: false, message: '必选', trigger: 'blur' }]">
							<!-- v-if="state.ruleForm.name != null" 加在是否的item上 -->
							<el-radio-group v-model="state.ruleForm.isenable">
								<el-radio :value="1">是</el-radio>
								<el-radio :value="0">否</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注">
							<el-input v-model="state.ruleForm.description" placeholder="请输入备注内容" clearable type="textarea" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button  @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="EditWarehouse">
import { reactive, ref } from 'vue';

import { getAPI } from '/@/utils/axios-utils';
import {  WarehouseApi } from '/@/api-services/apis/warehouse-api';
import { AccountTypeEnum, UpdateDictTypeInput } from '/@/api-services/models';
import { useUserInfo } from '/@/stores/userInfo';
import { WarehouseInput } from '/@/api-services/models/warehouse-input';
import { ElMessageBox, ElMessage } from 'element-plus';

// 打开弹窗
const openDialog = (row: any) => {
	state.ruleForm = JSON.parse(JSON.stringify(row));
	state.isShowDialog = true;
	ruleFormRef.value?.resetFields();
};

const props = defineProps({
	title: String,
});
const emits = defineEmits(['handleQuery']);
const ruleFormRef = ref();
const state = reactive({
	isShowDialog: false,
	ruleForm: {} as WarehouseInput,
});

// 关闭弹窗
const closeDialog = () => {
	emits('handleQuery');
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 提交
const submit = () => {
	ruleFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
			try{	
		if (state.ruleForm.id != undefined && state.ruleForm.id > 0) {
			
			await getAPI(WarehouseApi).apiWarehouseUpdatePost(state.ruleForm);
			ElMessage.success('编辑成功');
		} else {
			await getAPI(WarehouseApi).apiWarehouseAddPost(state.ruleForm);
			ElMessage.success('添加成功');

		}
		closeDialog();
	}
	catch (error) {
				//.error('删除失败:', error);
				ElMessage.error('失败，请稍后重试');
			}
		})
	}

// 导出对象
defineExpose({ openDialog });
</script>
